/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.momarketing.entity.MicroProductCategory;
import com.boxin.ims.modules.momarketing.entity.Project;
import com.boxin.ims.modules.momarketing.service.MicroProductCategoryService;
import com.boxin.ims.modules.momarketing.service.ProjectService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 微网站产分类Controller
 * @author Jakemanse
 * @version 2013-11-25
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/microProductCategory")
public class MicroProductCategoryController extends BaseController {

	@Autowired
	private MicroProductCategoryService microProductCategoryService;
	
	@Autowired
	private ProjectService projectService;
	
	@ModelAttribute
	public MicroProductCategory get(@RequestParam(required=false) Long id) {
		if (id != null){
			return microProductCategoryService.get(id);
		}else{
			return new MicroProductCategory();
		}
	}
	
	@RequiresPermissions("momarketing:microProductCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(MicroProductCategory microProductCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			microProductCategory.setUser(user);
		}
		
		Project  project = projectService.getByUserId(user.getId());
		if(project == null){
			
		}
		microProductCategory.setProject(project);
		
        Page<MicroProductCategory> page = microProductCategoryService.find(new Page<MicroProductCategory>(request, response), microProductCategory); 
        model.addAttribute("page", page);
		return "modules/momarketing/microProductCategoryList";
	}

	@RequiresPermissions("momarketing:microProductCategory:view")
	@RequestMapping(value = "form")
	public String form(MicroProductCategory microProductCategory, Model model) {
	
		User user = UserUtils.getUser();
		if (microProductCategory.getUser() == null || microProductCategory.getUser().getId() <1){
			microProductCategory.setUser(user);
		}
		
		Project  project = projectService.getByUserId(user.getId());
		if(project == null){
			model.addAttribute("microProductCategory", microProductCategory);
			addMessage(model, "请先创建微网站后再添加产品种类.");
			return "modules/momarketing/microProductCategoryForm";
		}
		microProductCategory.setProject(project);
		
		model.addAttribute("microProductCategory", microProductCategory);
		return "modules/momarketing/microProductCategoryForm";
	}

	@RequiresPermissions("momarketing:microProductCategory:edit")
	@RequestMapping(value = "save")
	public String save(MicroProductCategory microProductCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, microProductCategory)){
			return form(microProductCategory, model);
		}
		
		//首先创建微网站
		Project  project = microProductCategory.getProject();
		if(project == null || project.getId() <0){
			return form(microProductCategory,model);
		}
		
		
		microProductCategoryService.save(microProductCategory);
		addMessage(redirectAttributes, "保存微网站产分类'" + microProductCategory.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/microProductCategory/?repage";
	}
	
	
	
	@RequiresPermissions("momarketing:microProductCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		microProductCategoryService.delete(id);
		addMessage(redirectAttributes, "删除微网站产分类成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/microProductCategory/?repage";
	}

}
